package com.ss.android.article.base.feature.detail.model;

import com.bytedance.common.utility.Logger;
import com.ss.android.account.model.SpipeUser;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.common.AbsApiThread;
import com.ss.android.image.model.ImageInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public final class ArticleDetail {
    public NovelData novelData;
    public long mediaUserId;
    public Article article;
    public long itemId;
    public boolean delete;
    public String content;
    public long expireSeconds;
    public long timestamp;
    public String cacheToken;
    public List<ImageInfo> imageDetail;
    public String extJson;
    public String mediaInfo;
    public String h5Extra;
    public List galleryImageInfoList;
    public String pictureListItem;
    public boolean isOriginal;
    public long mediaId;
    public String name;
    public String avatarUrl;
    public boolean userVerified;
    public boolean canBePraised;
    public PraiseData praiseData;
    public boolean isWenda;
    public String etag;
    public String wendaExtra;
    public String qid;
    public SpipeUser spipeUser;
    public String novelDataJson;

    public ArticleDetail() {
        this.pictureListItem = "";
    }

    public final void parseGalleryImageInfoList(JSONArray galleryJSONArray) {
        GalleryImageInfo galleryImageInfo;
        if (galleryJSONArray != null && galleryJSONArray.length() != 0) {
            if (this.galleryImageInfoList == null) {
                this.galleryImageInfoList = new ArrayList();
            } else {
                this.galleryImageInfoList.clear();
            }

            int length = galleryJSONArray.length();
            for (int i = 0; i < length; ++i) {
                try {
                    galleryImageInfo = GalleryImageInfo.fromJSONObject(galleryJSONArray.getJSONObject(i));
                    if (galleryImageInfo != null) {
                        boolean valid;
                        if (android.support.design.a.isTextEmpty(galleryImageInfo.subTitle)) {
                            valid = false;
                        } else if (galleryImageInfo.subAbstract == null) {
                            valid = false;
                        } else if (galleryImageInfo.subImage == null) {
                            valid = false;
                        } else if (galleryImageInfo.subImage.isValid()) {
                            valid = true;
                        } else {
                            valid = false;
                        }
                        if (valid) {
                            this.galleryImageInfoList.add(galleryImageInfo);
                        }
                    }
                    continue;
                } catch (Exception exception) {
                    exception.printStackTrace();
                    break;
                }
            }
            this.pictureListItem = galleryJSONArray.toString();
        }
    }

    public final void parseWendaExtra(String json) {
        if (!android.support.design.a.isTextEmpty(json)) {
            try {
                JSONObject jo = new JSONObject(json);
                jo.put("wd_version", "1");
                this.wendaExtra = jo.toString();
                jo.optString("title");
                this.qid = jo.optString("qid");
                jo.optString("show_time");
                this.etag = jo.optString("etag");
                this.spipeUser = SpipeUser.parseUser(jo.optJSONObject("user"));
            } catch (JSONException e) {
            }
        }
    }

    public final void appendExtraData(JSONObject jo) {
        this.parseGalleryImageInfoList(jo.optJSONArray("gallery"));
        JSONObject h5ExtraJo = jo.optJSONObject("h5_extra");
        if (h5ExtraJo != null) {
            this.isOriginal = h5ExtraJo.optBoolean("is_original");
            this.h5Extra = h5ExtraJo.toString();
        }

        JSONObject mediaInfoJO = jo.optJSONObject("media_info");
        if (mediaInfoJO != null) {
            this.mediaInfo = mediaInfoJO.toString();
            if (mediaInfoJO != null) {
                this.mediaId = mediaInfoJO.optLong("media_id");
                this.name = mediaInfoJO.optString("name");
                this.userVerified = mediaInfoJO.optBoolean("user_verified");
                this.avatarUrl = mediaInfoJO.optString("avatar_url");
            }
        }

        if (h5ExtraJo != null) {
            h5ExtraJo = h5ExtraJo.optJSONObject("media");
            if (h5ExtraJo != null) {
                this.canBePraised = AbsApiThread.optBoolean(h5ExtraJo, "can_be_praised", false);
                this.praiseData = PraiseData.fromJSONObject(h5ExtraJo);
            }
        }

        this.mediaUserId = jo.optLong("media_user_id", 0);
        h5ExtraJo = jo.optJSONObject("novel_data");
        if (h5ExtraJo != null) {
            this.novelData = new NovelData();
            this.novelData.initWithJSONObject(h5ExtraJo);
            this.novelDataJson = h5ExtraJo.toString();
        }

        this.isWenda = jo.optInt("is_wenda") > 0;
        this.parseWendaExtra(jo.optString("wenda_extra"));
        mediaInfoJO = new JSONObject();
        try {
            if (!android.support.design.a.isTextEmpty(this.pictureListItem)) {
                mediaInfoJO.put("picture_list_item", this.pictureListItem);
            }

            if (!android.support.design.a.isTextEmpty(this.h5Extra)) {
                mediaInfoJO.put("h5_extra", this.h5Extra);
            }

            if (!android.support.design.a.isTextEmpty(this.mediaInfo)) {
                mediaInfoJO.put("media_info", this.mediaInfo);
            }

            mediaInfoJO.put("is_wenda", this.isWenda ? 1 : 0);
            mediaInfoJO.put("wenda_etag", this.etag);
            mediaInfoJO.put("media_user_id", this.mediaUserId);
            if (!android.support.design.a.isTextEmpty(this.wendaExtra)) {
                mediaInfoJO.put("wenda_extra", this.wendaExtra);
            }
        } catch (JSONException jsonException) {
            Logger.d("ArticleDetail", "exception in appendExtraData : " + jsonException.toString());
        }
        this.extJson = mediaInfoJO.toString();
    }

    public final boolean hasGalleryImages() {
        return this.galleryImageInfoList != null && this.galleryImageInfoList.size() > 0;
    }

    public static final class NovelData {
        public String preGroupUrl;
        public String nextGroupUrl;
        public String url;
        public int serialCount;
        private long bookid;

        public NovelData() {

        }

        public final void initWithJSONObject(JSONObject jo) {
            if (jo != null) {
                this.bookid = jo.optLong("book_id");
                jo.optLong("pre_group_id");
                jo.optLong("pre_item_id");
                jo.optLong("next_group_id");
                jo.optLong("next_item_id");
                this.preGroupUrl = jo.optString("pre_group_url");
                this.nextGroupUrl = jo.optString("next_group_url");
                this.url = jo.optString("url");
                this.serialCount = jo.optInt("serial_count");
                jo.optInt("type");
            }
        }

        public final String getBookId() {
            return "s_" + this.bookid;
        }
    }
}

